<template>
  <view>
    <!-- 选择地址区域 -->

    <!-- 商品列表区域 -->
    <block v-for="product in cartItems" :key="product.goods_id">
      <view class="goods-item">
        <!-- 商品左侧图片区域 -->
        <view class="goods-item-left">
          <radio :checked="product.goods_state" color="#C00000"></radio>
          <image
            :src="product.goods_small_logo || defaultImage"
            class="goods-pic"
          ></image>
        </view>

        <!-- 商品右侧信息区域 -->
        <view class="goods-item-right">
          <!-- 商品标题 -->
          <view class="goods-name">{{ product.goods_name }}</view>
          <view class="goods-info-box">
            <!-- 商品价格 -->
            <view class="goods-price"
              >￥{{ $formatP(product.goods_price) }}</view
            >
          </view>
        </view>
      </view>
    </block>

    <!-- 模拟选择收货地址 -->
    <button type="primary" size="mini" @click="chooseAddress">获取地址</button>

    {{ address.provinceName }} {{ address.detailInfo }}

    <br />

    <!-- 模拟一键登录 -->
    <button type="warn" size="mini" @click="login">一键登录</button>

    <!-- 模拟支付 -->
    <button size="mini" @click="pay">支付</button>
  </view>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      defaultImage:
        'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',

      address: {},
    }
  },
  computed: {
    ...mapState('cart', ['cartItems']),
  },
  methods: {
    async chooseAddress() {
      // 调用小程序的api获取收货地址
      const res = await uni.chooseAddress()
      // console.log(res)
      this.address = res
    },

    async login() {
      // 正常登录流程: 输入手机号 验证码  拿着数据请求接口 获取token
      // 一键登录:
      //   1. encryptedData rawData iv signature             wx.getUserProfile() 获取到用户资料
      //   2. code                                           wx.login()  获取登录凭证
      //   3. encryptedData rawData iv signature code        请求后端接口 获取token

      const res = await uni.getUserProfile({
        desc: '一键登录', // 声明获取用户个人信息后的用途 必须的!!!
      })
      console.log(res)

      const res2 = await uni.login()
      console.log(res2)

      // 到这以后 就和正常登录一样的 请求后端接口就可以了...
    },

    pay() {
      // 完成支付:
    },
  },
}
</script>

<style lang="scss">
.goods-item {
  display: flex;
  padding: 20rpx 10rpx;
  border-bottom: 1rpx solid #f0f0f0;
  width: 750rpx;
  box-sizing: border-box;

  .goods-item-left {
    margin-right: 10rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .goods-pic {
      width: 200rpx;
      height: 200rpx;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .goods-name {
      font-size: 26rpx;
    }

    .goods-price {
      font-size: 32rpx;
      color: #c00000;
    }
  }
}
</style>
